package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Slf4j
@Api(tags = "工具接口")
public class common {
    @PostMapping("/upload")
    @ApiOperation("保存文件")
    public Result upload(MultipartFile file){
        if (file.isEmpty()){
            return Result.error("上传失败");
        }
        //重命名
        String originalName = file.getOriginalFilename();
        String ext = "." + originalName.split("\\.")[1];
        String uuid = UUID.randomUUID().toString().replace("-","");
        String fileName = uuid + ext;
        //上传图片
        ApplicationHome applicationHome = new ApplicationHome(this.getClass());
        String pre = "C:\\Users\\linchuncheng\\Desktop\\qian\\nginx-1.20.2\\html\\sky\\image\\";
        String path = pre + fileName;
        try {
            file.transferTo(new File(path));
        } catch (IOException e){
            e.printStackTrace();
        }
        String res = "http://localhost:70/image/" + fileName;
        return Result.success(res);
    }
}
